<?php
class PublicAction extends Action {
	public function checkLogin() {
		$User	=	D('User');
		if(empty($_POST['account'])) {
			$this->error('帐号错误！');
		}elseif (empty($_POST['password'])){
			$this->error('密码必须！');
		}elseif ($_SESSION['verify'] != md5($_POST['verify'])){
			$this->error('验证码错误！');
		}
        //生成认证条件
        $map            =   array();
        $map["account"]	=	$_POST['account'];
        $map["status"]	=	array('gt',0);
	
		$authInfo = $User->find($map);
        //使用用户名、密码和状态的方式进行认证
        if(false === $authInfo) {
            $this->error('用户名不存在或已禁用！');
        }else {
			if($authInfo['account']	!=  $_POST['account']) {
				$this->error('帐号错误！');
			}
            if($authInfo['password'] != md5($_POST['password'])) {
            	$this->error('密码错误！');
            }
			//加入USER_ID
			$_SESSION['USER_ID']=$authInfo['id'];
			$Right=D("Uright");
			$vo=$Right->find("`USER_ID`='".$_SESSION['USER_ID']."'");
			if(!$vo) 
			{
				$this->error('没有分配权限！无法登陆，请联系管理员！');
			}			
			else
			{
				//载入角色信息
				$Role = D("Role");
				$rolevo=$Role->find("`id`='".$vo['ROLE_ID']."'");
				//载入权限ID
				$_SESSION['user_right']	=	$vo['ROLE_ID'];

				//班主任	
				if ( $vo['ROLE_ID'] == 6)
				{
					$_SESSION['dm_yx']	=	$vo['DM_YX'];
					$_SESSION['dm_zy']	=	$vo['DM_ZY'];
					$_SESSION['dm_bj']	=	$vo['DM_BJ'];
					$_SESSION['rightInfo']=$rolevo['name'].":".$vo['YX']."-".$vo['ZY']."-".$vo['BJ'];			
				//系统管理员
				}
				elseif($vo['ROLE_ID'] == 1)
				{
					$_SESSION['rightInfo']=$rolevo['name'];			
				//其他角色
				}
				else
				{
					$_SESSION['dm_yx']	=	$vo['DM_YX'];				
					$_SESSION['rightInfo']=$rolevo['name'].":".$vo['YX'];					
				}
				
				
				//登入系统
				$this->success('登录成功！');
			}
		}
	}
	
	public function login() {
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$this->display();
		}else{
			$this->redirect('index','Index');
		}
	}

    public function logout() 
    {
        if(isset($_SESSION['user_right'])) {
			unset($_SESSION['rightInfo']);
			unset($_SESSION['user_right']);
			unset($_SESSION['dm_yx']);
			unset($_SESSION['dm_zy']);
			unset($_SESSION['dm_bj']);
            $this->assign("message",'登出成功！');
            $this->assign("jumpUrl",__URL__.'/login/');
        }else {
            $this->assign('error', '已经登出！');
        }
        $this->forward();
    }	

    /**
     +----------------------------------------------------------
     * 验证码显示
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    function verify() 
    {
        import("ORG.Util.Image");
        if(isset($_REQUEST['adv'])) {
        	Image::showAdvVerify(); 
        }else {
        	Image::buildImageVerify(); 
        }
    }

	public function index() 
	{
		//如果通过认证跳转到首页
		redirect(__APP__);
	}

    /**
     +----------------------------------------------------------
     * 取得操作成功后要返回的URL地址
     * 默认返回当前模块的默认操作 
     * 可以在action控制器中重载
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    function getReturnUrl() 
    {
        return __URL__.'?'.C('VAR_MODULE').'='.MODULE_NAME.'&'.C('VAR_ACTION').'='.C('DEFAULT_ACTION');
    }
    
}
?>